package com.visionet.waveletData.web.common.weather;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.visionet.waveletData.web.common.utilities.props.PropsKeys;
import com.visionet.waveletData.web.common.utilities.props.PropsUtil;
import com.visionet.waveletData.web.common.utils.Validator;

public class WeatherUtil {
	private static Logger logger = LoggerFactory.getLogger(WeatherUtil.class);
	
	private static String SERVICES_HOST = "www.webxml.com.cn";
	private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
	private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL
			+ "getRegionProvince";
	private static String CITY_CODE_URL = WEATHER_SERVICES_URL
			+ "getSupportCityString?theRegionCode=";
	private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
			+ "getWeather?theUserID=&theCityCode=";

	private WeatherUtil() {
	}




	private static int getProvinceCode(String provinceName) throws Exception {
		Document document;
		DocumentBuilderFactory documentBF = DocumentBuilderFactory
				.newInstance();
		documentBF.setNamespaceAware(true);
		int provinceCode = 0;
		DocumentBuilder documentB = documentBF.newDocumentBuilder();
		InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); // 具体webService相关
		document = documentB.parse(inputStream);
		NodeList nodeList = document.getElementsByTagName("string"); // 具体webService相关
		int len = nodeList.getLength();
		for (int i = 0; i < len; i++) {
			Node n = nodeList.item(i);
			String result = n.getFirstChild().getNodeValue();
			String[] address = result.split(",");
			String pName = address[0];
			String pCode = address[1];
			if (pName.equalsIgnoreCase(provinceName)) {
				provinceCode = Integer.parseInt(pCode);
			}
		}
		inputStream.close();
		return provinceCode;
	}

	private static int getCityCode(int provinceCode, String cityName)
			throws Exception {
		Document doc;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		int cityCode = 0;
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode); // 具体webService相关
		doc = db.parse(is);
		NodeList nl = doc.getElementsByTagName("string"); // 具体webService相关
		int len = nl.getLength();
		for (int i = 0; i < len; i++) {
			Node n = nl.item(i);
			String result = n.getFirstChild().getNodeValue();
			String[] address = result.split(",");
			String cName = address[0];
			String cCode = address[1];
			if (cName.equalsIgnoreCase(cityName)) {
				cityCode = Integer.parseInt(cCode);
			}
		}
		is.close();
		return cityCode;
	}

	private static InputStream getSoapInputStream(String url) {
		InputStream inputStream = null;
		try {
			URL urlObj = new URL(url);
			URLConnection urlConn = urlObj.openConnection();
			urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
			urlConn.connect();
			inputStream = urlConn.getInputStream();
		} catch (MalformedURLException e) {
			logger.error("WeatherUtil.getSoapInputStream MalformedURLException:"+e.toString());
		} catch (IOException e) {
			logger.error("WeatherUtil.getSoapInputStream IOException:"+e.toString());
		}
		return inputStream;
	}

}